package com.smxy.mall.service.impl;

import com.smxy.mall.dao.AddressDao;
import com.smxy.mall.entity.Address;
import com.smxy.mall.model.Current;
import com.smxy.mall.model.request.AddressReq;
import com.smxy.mall.service.AddressService;
import com.smxy.mall.utils.Common;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {

    @Resource
    private AddressDao addressDao;

    @Override
    public Address save(AddressReq addressReq, Current current) {
        Address address = new Address();
        address.setUserId(current.getUserId());
        address.setName(addressReq.getName());
        address.setPhone(addressReq.getPhone());
        address.setAddress(addressReq.getAddress());
        address.setDetail(addressReq.getDetail());
        Integer sex = addressReq.getSex();
        address.setSex(sex);
        address.setTag(addressReq.getTag());
        if(Common.isId(addressReq.getId()+"")){
            address.setId(addressReq.getId());
        }
        return addressDao.save(address);
    }

    @Override
    public List<Address> list() {
        return addressDao.findAll();
    }

    @Override
    public List<Address> findByUserId(Integer userId) {
        return addressDao.findByUserId(userId);
    }

    @Override
    public Address findById(Integer id) {
        return addressDao.findById(id);
    }
}
